package com.sunny.ynhz.utils;

/**
 * Created by Administrator on 2017/7/13/013.
 */

import com.sunny.ynhz.call.ICutDownTime;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 对应的显示页封装
 */
public class SpashTimeUtils {
    ICutDownTime iCutDownTime;
    private Subscription subscribe;

    public SpashTimeUtils(ICutDownTime iCutDownTime) {
        this.iCutDownTime = iCutDownTime;
    }

    /**
     * 开始倒计时
     */
    public void startCutDown() {
        subscribe = Observable.interval(0, 1, TimeUnit.SECONDS).map(new Func1<Long, Integer>() {
            @Override
            public Integer call(Long aLong) {
                int i = 3 - aLong.intValue();
                return i;
            }
        }).take(5).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        iCutDownTime.onSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Integer integer) {
                        iCutDownTime.onProgress(integer);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();

                    }
                });
    }

    /**
     * 取消订阅
     */
    public void canceSubscribe() {
        if (subscribe != null) {
            subscribe.unsubscribe();
        }
    }


}
